def compress_graph(G):
    # 压缩网络拓扑图
    while True:
        # 合并度为1的节点
        nodes_to_remove = []
        for node in G.nodes():
            if G.degree(node) == 1:
                nodes_to_remove.append(node)
        G.remove_nodes_from(nodes_to_remove)

        # 合并度为2的节点
        nodes_to_remove = []
        for node in G.nodes():
            if G.degree(node) == 2:
                neighbors = list(G.neighbors(node))
                neighbor1, neighbor2 = neighbors[0], neighbors[1]
                if G.has_edge(neighbor1, neighbor2):
                    G[neighbor1][neighbor2]['weight'] += (G[node][neighbor2]['weight']
                                                          * G[node][neighbor1]['weight']) / 2
                    nodes_to_remove.append(node)
        G.remove_nodes_from(nodes_to_remove)

        # 判断网络拓扑图是否收敛
        if len(nodes_to_remove) == 0:
            break
